
package com.eworking.eim.cloud;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;

class CustomMultiPartEntity extends MultipartEntity {
    
    private final ProgressListener progressListener;
    
    public CustomMultiPartEntity(ProgressListener paramProgressListener) {
        this.progressListener = paramProgressListener;
    }
    
    public CustomMultiPartEntity(HttpMultipartMode paramHttpMultipartMode, ProgressListener paramProgressListener) {
        super(paramHttpMultipartMode);
        this.progressListener = paramProgressListener;
    }
    
    public CustomMultiPartEntity(HttpMultipartMode paramHttpMultipartMode, String paramString, Charset paramCharset,
        ProgressListener paramProgressListener) {
        super(paramHttpMultipartMode, paramString, paramCharset);
        this.progressListener = paramProgressListener;
    }
    
    public void writeTo(OutputStream paramOutputStream) throws IOException {
        super.writeTo(new CountingOutputStream(paramOutputStream, this.progressListener));
    }
    
    public static class CountingOutputStream extends FilterOutputStream {
        
        private final CustomMultiPartEntity.ProgressListener listener;
        
        private long transferred = 0;
        
        public CountingOutputStream(OutputStream paramOutputStream,
            CustomMultiPartEntity.ProgressListener paramProgressListener) {
            super(paramOutputStream);
            this.listener = paramProgressListener;
            this.transferred = 0;
        }
        
        public void write(byte[] paramArrayOfByte, int paramInt1, int paramInt2) throws IOException {
            this.out.write(paramArrayOfByte, paramInt1, paramInt2);
            this.transferred += paramInt2;
            this.listener.transferred(this.transferred);
        }
        
        public void write(int paramInt) throws IOException {
            this.out.write(paramInt);
            this.transferred += 1;
            this.listener.transferred(this.transferred);
        }
    }
    
    public static abstract interface ProgressListener {
        
        public abstract void transferred(long paramLong);
    }
}
